package common

import (
	"hash/fnv"
	"strconv"
)

// 自定义任务类型
type JobPhase string

const (
	MapPhase    JobPhase = "Map"
	ReducePhase JobPhase = "Reduce"
)

// 用于保存需要传递给map和reduce的key-value对
type KeyValue struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

// 生成结果文件名称
func GetMergeName(jobName string, reduceTask int) string {
	return "out/mrtmp." + jobName + "-res-" + strconv.Itoa(reduceTask)
}

// 生成中间文件名称
func GetReduceName(jobName string, mapTask int, reduceTask int) string {
	return "out/mrtmp." + jobName + "-" + strconv.Itoa(mapTask) + "-" + strconv.Itoa(reduceTask)
}

func IHash(s string) int {
	h := fnv.New32a()
	h.Write([]byte(s))

	return int(h.Sum32() & 0x7fffffff)
}
